package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 发送短信
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){

        //获取手机号
        String phone = user.getPhone();

        if (StringUtils.isNotEmpty(phone)){

            //生成随机验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("验证码: {}",code);

            //发送短信
//            SMSUtils.sendMessage("黑马旅游网","SMS_198917972",phone,code);

            //将验证码存入session
//            session.setAttribute("phone",code);

            //将验证码存入redis,设置验证码过期时间
            redisTemplate.opsForValue().set(phone,code,1, TimeUnit.MINUTES);

            return R.success("手机验证码发送成功");
        }

        return R.error("验证码发送失败");

    }


    /**
     * 登录验证
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){

        log.info(map.toString());
        //获取手机号和验证码
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();

//        Object codeInSession = session.getAttribute("phone");
        //从redis中获取验证码
        String codeInSession = (String) redisTemplate.opsForValue().get(phone);

        //判断验证码是否正确
        if ( codeInSession != null && codeInSession.equals(code)){
            //判断是否注册过
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(queryWrapper);

            if (user == null){
                //未注册
                user = new User();
                user.setPhone(phone);

                userService.save(user);
            }
            //将用户信息加入session
            session.setAttribute("user",user.getId());

            //登录成功，将redis中验证码清除
            redisTemplate.delete(phone);

            return R.success(user);
        }

        return R.error("登录失败");
    }





}
